package com.dingding.special.order.publish.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.facade.queue.facade.IQueueFacade;
import com.dingding.facade.special.ISpecialOrderFacade;
import com.dingding.facade.special.trasfer.place.ReqBackstagePlace;
import com.dingding.facade.special.trasfer.place.ResBackstagePlace;
import com.dingding.facade.special.trasfer.route.RouteDto;
import com.dingding.facade.user.facade.IUserOperateFacade;
import com.dingding.special.common.exception.bussiness.BizExpEnum;
import com.dingding.special.common.exception.bussiness.BussinessException;
import com.dingding.special.common.exception.server.ServerException;
import com.dingding.special.common.exception.server.ServerExpEnum;
import com.dingding.special.order.publish.biz.IPublishSpecialOrder;
import com.dingding.special.order.publish.dao.IPublishSpecialOrderDao;

/**
 * @Description:发布专车单
 * @author fengshuonan
 * @date 2016年9月13日 下午3:39:01
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("publishSpecialOrderBackstage")
public class PublishSpecialOrderBackstage implements IPublishSpecialOrder{
	
	@Resource(name = "userOperateFacade")
	private IUserOperateFacade userOperateFacade;
	
	@Resource(name = "publishSpecialOrderDao")
	private IPublishSpecialOrderDao publishSpecialOrderDao;
	
	@Resource(name = "specialOrderFacade")
	private ISpecialOrderFacade specialOrderFacade;
	
	@Resource(name = "queueFacade")
	private IQueueFacade queueFacade;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResBackstagePlace placeSpecialOrderBackstage(ReqBackstagePlace reqBackstagePlace) {
		ResBackstagePlace response = new ResBackstagePlace();
		
		/**
		 * 获取该用户的用户信息
		 */
		UserLogin ul = this.userOperateFacade.getOrAddUserByTravelAgc(reqBackstagePlace.getCusTeleInput());
		if(ul == null){
			throw new ServerException(ServerExpEnum.SERVER_DATABASE_EXCEPTION);
		}
		
		/**
		 * 判断该用户有没有进行中订单
		 */
		Boolean flag = this.publishSpecialOrderDao.haveMatchingOrder(
				ul.getUid(),reqBackstagePlace.getRealSelect());
		if(flag == true){//有进行中订单
			throw new BussinessException(BizExpEnum.HAVE_MATCHING_ORDER);
		}
		
		/**
		 * 根据请求信息生成一个完成的UserSpecialOrderT实例
		 */
		UserspecialorderT ordert = this.publishSpecialOrderDao.generateUserSpecialOrder(ul.getUid(), reqBackstagePlace);
		
		/**
		 * 添加订单到临时表
		 */
		this.publishSpecialOrderDao.saveToDb(ordert);
		
		/**
		 * 发单成功后，旅行社的权益次数减一
		 */
		this.publishSpecialOrderDao.downOneUseNum(ordert.getDepId(),ordert.getRouteId());
		
		/**
		 * 添加这个订单到指派订单队列
		 */
		this.queueFacade.addSpecialOrderToQueue(ordert);
		
		response.setCode(200);
		response.setMessage("请求成功");
		return response;
	}

	@Override
	@Transactional(readOnly = true)
	public List<RouteDto> getRoutes(String depId) {
		return this.publishSpecialOrderDao.getRoutes(depId);
	}
}
